Skip to content

Method: addPendingReference(String, Object, Field)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.deserialization.reference;
16:
17: import cz.cvut.kbss.jsonld.exception.UnresolvedReferenceException;
18:
19: import java.lang.reflect.Field;
20: import java.util.*;
21:
22: /**
23: * Registry of pending references.
24: */
25: public class PendingReferenceRegistry {
26:
27: private final Map<String, Set<PendingReference>> pendingReferences = new HashMap<>();
28:
29: /**
30: * Registers a pending reference with the specified identifier.
31: *
32: * @param identifier Reference identifier
33: * @param targetObject Object which contains the referring attribute
34: * @param targetField Field representing the target attribute
35: */
36: public void addPendingReference(String identifier, Object targetObject, Field targetField) {
37:• assert identifier != null;
38:• assert targetObject != null;
39:• assert targetField != null;
40: addReference(identifier, new SingularPendingReference(targetObject, targetField));
41: }
42:
43: private void addReference(String identifier, PendingReference reference) {
44: final Set<PendingReference> refs = pendingReferences.computeIfAbsent(identifier, (id) -> new HashSet<>());
45: refs.add(reference);
46: }
47:
48: /**
49: * Registers a pending reference with the specified identifier.
50: *
51: * @param identifier Reference identifier
52: * @param targetObject Collection referencing the object
53: */
54: public void addPendingReference(String identifier, Collection targetObject) {
55: assert identifier != null;
56: assert targetObject != null;
57: addReference(identifier, new CollectionPendingReference(targetObject));
58: }
59:
60: /**
61: * Resolves the pending references by replacing them with the specified full object.
62: * <p>
63: * This method goes through the pending references and sets the specified {@code referencedObject} on the
64: * corresponding target objects.
65: *
66: * @param identifier Identifier of the referenced object
67: * @param referencedObject The referenced object
68: * @throws cz.cvut.kbss.jsonld.exception.TargetTypeException If the {@code referencedObject} cannot be assigned to a
69: * target field due to type mismatch
70: */
71: public void resolveReferences(String identifier, Object referencedObject) {
72: assert identifier != null;
73: assert referencedObject != null;
74: final Set<PendingReference> refs = pendingReferences.remove(identifier);
75: if (refs != null) {
76: refs.forEach(pr -> pr.apply(referencedObject));
77: }
78: }
79:
80: /**
81: * Checks whether any pending unresolved references are left.
82: *
83: * @throws UnresolvedReferenceException Thrown when pending references exist
84: */
85: public void verifyNoUnresolvedReferencesExist() {
86: if (!pendingReferences.isEmpty()) {
87: throw new UnresolvedReferenceException(
88: "There are unresolved references to objects " + pendingReferences.keySet());
89: }
90: }
91: }